/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.rehan.commons;

import java.io.File;
import java.util.Properties;
import org.safehaus.uuid.EthernetAddress;
import org.safehaus.uuid.NativeInterfaces;
import org.safehaus.uuid.UUID;
import org.safehaus.uuid.UUIDGenerator;

/**
 *
 * @author Martin Rehanek (rehan@mzk.cz)
 */
public class EthAddrUuidGenerator implements UuidGenerator {

    private static final String PROP_LIB_DIR = "EtherAddrLibDir";
    private static EthAddrUuidGenerator instance = null;
    private final UUIDGenerator generator;
    private final EthernetAddress ethernetAddress = NativeInterfaces.getPrimaryInterface();

    public static EthAddrUuidGenerator instanceOf(Properties properties) {
        if (instance == null) {
            instance = new EthAddrUuidGenerator(properties);
        }
        return instance;
    }

    private EthAddrUuidGenerator(Properties properties) {
        generator = UUIDGenerator.getInstance();
        PropertyLoader propertyLoader = new PropertyLoader(properties);
        File libDir = propertyLoader.loadDir(PROP_LIB_DIR);
        NativeInterfaces.setLibDir(libDir);
    }

    public String generateUuid() {
        UUID generatedUuid = generator.generateTimeBasedUUID(ethernetAddress);
        return generatedUuid.toString();
    }
}
